// pages/my/vip/vip_feedBack/index.js
const addSuggest=require("../../controllers/ControllerX").controller.addSuggest;

const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    files: [],
    value:{
        content:"",
        contact:"",
        images:""
    },
    fileList:[],
    token:'',
    length:"",
    baseUrl:"https://www.xlshhr.com/beauty-industry"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getToken();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // },
  //获取反馈内容
  getText(e){
    let text=e.detail.value;
    this.setData({
        'value.content':text.substring(0,200),
        length:text.length
    })
  },

  // 意见反馈
  sendBack(){
    this.setData({
        'value.contact':wx.getStorageSync('mobile'),
        'value.shopId':wx.getStorageSync('shopId')
    })

  },
 // 图片上传
 uploadImg(){
    if(this.data.fileList.length<9){
        let that=this;
        wx.chooseImage({
        success (res) {
            const tempFilePaths = res.tempFilePaths;
            const tempFiles=res.tempFiles;
            if(tempFiles[0].size>1024*1024){
              wx.showToast({
                title: '单张图片大不能超过1MB',
                icon: 'none',
                duration: 2000
              })
              return
            }
            for(let i=0;i<9-that.data.fileList.length;i++){
              wx.uploadFile({
                url: that.data.baseUrl+'/api/img/uploadFile', 
                filePath: tempFilePaths[i],
                name: 'image',
                formData: {
                    'sessionId':that.data.token
                },
                success (res){
                    console.log(res);
                    // const data = res.date
                    let src=JSON.parse(res.data)
                    let srcList=that.data.fileList;
                    srcList.push(src.data);
                    that.setData({
                        fileList:srcList
                    })
                   }
                })
            }
        }
        })
    }
  },
  //获取token
  getToken(){
     this.setData({
         token:wx.getStorageSync("token")
     })
  },
  //提交反馈
  getAddSuggest(){
    let images=''
    for(let i=0;i<this.data.fileList.length;i++){
        images=images+"images="+this.data.fileList[i]+",";
    }
    images=images.slice(0,images.length-1);
    this.setData({
        'value.contact':wx.getStorageSync('mobile'),
        'value.images':images
    })
    if(this.data.value.content==""){
      wx.showToast({
        icon:"none",
        title: '意见反馈内容不能为空',
        duration:1000
      })
    }else{
      addSuggest(this.data.value).then(res=>{
        if(res.code==1){
            wx.showToast({
              title: '意见提交成功',
              duration:1000
            })
            setTimeout(function(){
              wx.navigateBack()
            },1000)
        }
    })
    }
  },

  //删除将要上传的图片
  deleteImg(e){
    let index=e.currentTarget.dataset.index;
    let fileList=this.data.fileList;
    fileList.splice(index,1);
    this.setData({
      fileList:fileList
    })
  }
})